package com.ruoyi.mateInventory.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateInventory.domain.MateInventory;
import com.ruoyi.mateInventory.domain.MateInventoryPlan;
import com.ruoyi.mateInventory.service.MateInventoryService;
import com.ruoyi.material.domain.MateProductModel;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.service.MateManufactuterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/MateInventory")
public class MateInventoryController extends BaseController {

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    MateInventoryService mateInventoryService;

    /**
     * 下拉表格
     */
    @GetMapping("/selectTableRow")
    public JSONObject tableSelect(@RequestParam(value = "mateProductType", required = false) String mateProductType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "");
        Map map = new HashMap<String, Object>();
        map.put("mateProductType", mateProductType);
        map.put("companyId", SecurityUtils.getLoginUser().getCompanyId());

        List<MaterialProduct> products = mateInventoryService.selectProducts(map);
        JSONArray array = new JSONArray();
        for (MaterialProduct product : products) {
            if (product.getMateProductModel().size() != 0) {
                List<MateProductModel> productModels = product.getMateProductModel();
                for (MateProductModel productModel : productModels) {
                    JSONObject obj = new JSONObject();
                    obj.put("materialName", product.getMaterialName());
                    obj.put("mateCategory", product.getMateCategory());
                    obj.put("materialId", product.getMaterialId());
                    obj.put("mateModelCode", productModel.getMateModelCode());
                    obj.put("mateModeno", productModel.getMateModeno());
                    obj.put("mateProductName", product.getMateProductName());
                    obj.put("mateproductId", product.getMateproductId());
                    obj.put("mateProductType", productModel.getMateProductType());
                    array.add(obj);
                }
            }
        }
        jsonObject.put("data", array);
        return jsonObject;
    }

    @PostMapping("/addMateInventory")
    public AjaxResult addMateInventory(@RequestBody MateInventory mateInventory) {
        mateInventory.setDeptId(SecurityUtils.getDeptId());
        int a = mateInventoryService.addMateInventory(mateInventory);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @GetMapping("/MateInventoryList")
    public TableDataInfo selectMateInventoryList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                                 @RequestParam(value = "endTime", required = false) String endTime) {

        MateInventory mateInventory = new MateInventory();
        mateInventory.setBeginTime(beginTime);
        mateInventory.setEndTime(endTime);
        mateInventory.setDeptId(SecurityUtils.getDeptId());
        startPage();
        List<MateInventory> list = mateInventoryService.selectMateInventoryList(mateInventory);
        return getDataTable(list);
    }

    @GetMapping("/MateInventoryPlan")
    public TableDataInfo MateInventoryPlan(@RequestParam(value = "beginTime", required = false) String beginTime,
                                           @RequestParam(value = "endTime", required = false) String endTime) {
        MateInventoryPlan mateInventoryPlan = new MateInventoryPlan();
        mateInventoryPlan.setBeginTime(beginTime);
        mateInventoryPlan.setEndTime(endTime);
        mateInventoryPlan.setDeptId(SecurityUtils.getDeptId());
        startPage();
        List<MateInventoryPlan> list = mateInventoryService.MateInventoryPlan(mateInventoryPlan);
        return getDataTable(list);
    }

    @GetMapping("/selectEdit")
    public MateInventory selectEdit(@RequestParam(value = "inventoryId", required = false) Long inventoryId) {
        MateInventory mateInventory = mateInventoryService.selectEdit(inventoryId);
        return mateInventory;
    }

    @PostMapping("/updateMateInventory")
    public AjaxResult updateMateInventory(@RequestBody MateInventory mateInventory) {
        mateInventory.setDeptId(SecurityUtils.getDeptId());
        int a = mateInventoryService.updateMateInventory(mateInventory);
        if (a > 0) {
            return AjaxResult.success("修改信息成功");
        } else {
            return AjaxResult.error("修改信息失败");
        }
    }

    @GetMapping("/remove")
    public AjaxResult remove(@RequestParam(value = "inventoryId", required = false) Long inventoryId) {
        mateInventoryService.removeMateInventory(inventoryId);
        return AjaxResult.success("删除成功");

    }

    @GetMapping("/removePlan")
    public AjaxResult removePlan(@RequestParam(value = "inventoryId", required = false) Long inventoryId) {
        mateInventoryService.removePlan(inventoryId);
        return AjaxResult.success("删除成功");

    }

    @GetMapping("/AuditMateInventory")
    public List<MateInventory> AuditMateInventory() {
        List<MateInventory> mateInventory = mateInventoryService.AuditMateInventory(SecurityUtils.getDeptId());
        return mateInventory;
    }

   /*
   保存采购计划
    */
    @PostMapping("/addMateInventoryPlan")
    public AjaxResult addMateInventoryPlan(@RequestBody MateInventoryPlan mateInventoryPlan) {

        mateInventoryPlan.setDeptId(SecurityUtils.getDeptId());
        int a = mateInventoryService.addMateInventoryPlan(mateInventoryPlan);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }


        @GetMapping("/selectPlan")
    public MateInventoryPlan selectPlan(@RequestParam(value = "inventoryPlanId", required = false) Long inventoryPlanId) {
        MateInventoryPlan mateInventoryPlan = mateInventoryService.selectPlan(inventoryPlanId);
        return mateInventoryPlan;
    }

}
